<?php
class Module_model extends MY_Model{
	
	function __construct(){
		parent::__construct();
		$this->_table      = 'modules';
		$this->primary_key = 'id';
	}
	
	function get_by_parent_id($parent_id = 0){
		$q		=	$this->db->select()->from($this->_table)->where('parent_id',$parent_id)->order_by('order ASC')->get();
		$rows	=	$q->result();
		$q->free_result();
		return $rows;
	}
	
	function get_by_group_id($group_id,$parent_id = 0){
		/*
		$q	=	$this->db->select('m.name,m.title,m.id,m.parent_id,m.css_class')->from('modules as m')->join('permissions as p','m.id=p.module_id')->where('group_id',$group_id)->where('m.parent_id',$parent_id)->group_by('m.name,m.id')->order_by('m.order ASC')->get();
    	$rows	=	$q->result();
    	$q->free_result();
    	*/
		$q	=	$this->db->select('m.name,m.title,m.id,per_id,m.parent_id,m.css_class,p.method,p.status')->from('modules as m')->join('permissions as p','p.module_id=m.id')->WHERE('group_id',$group_id)->order_by('m.order ASC')->get();
    	$rows	=	$q->result();
    	$q->free_result();
    	//print_r($this->db->last_query());
    	return $rows;
	}
	
	function get_by_id($id)
	{
		$q	=	$this->db->select('m.name,m.title,m.id,m.parent_id,m.css_class,p.method,p.module_id,p.status')->from('modules as m')->join('permissions as p','p.module_id=m.id')->WHERE(array('per_id' => $id))->get();
    	$row	=	$q->row();
    	$q->free_result();
    	return $row;
	}
	
	function get_non_group_id($group_id){
		//$q	=	$this->db->select('m.name,m.title,m.id,m.parent_id,m.css_class')->from('modules as m')->join('permissions as p','m.id=p.module_id')->where('group_id <>',$group_id)->WHERE('p.module_id NOT IN(SELECT module_id FROM permissions WHERE group_id='.$group_id.')')->group_by('m.name,m.id,p.module_id')->order_by('m.order ASC')->get();
		$q	=	$this->db->select('m.name,m.title,m.id,m.parent_id,m.css_class')->from('modules as m')->join('permissions as p','p.module_id=m.id')->WHERE('group_id',$group_id)->group_by('m.name,m.id')->order_by('m.order ASC')->get();
    	$rows	=	$q->result();
    	$q->free_result();
    	return $rows;
	}
	
	function check_action($group_id,$method,$act){
		$q	=	$this->db->select('p.method,p.status')->from('modules as m')->join('permissions as p','m.id=p.module_id')->where('group_id',$group_id)->where('m.name',$method)->get();
		$rows = $q->result();
		$allow = false;
		foreach($rows as $r){
			if((($r->method == $act || $r->method == '*') && $r->status == 'active')) $allow = true;
			if(($r->method == $act && $r->status == 'inactive')) {$allow =false;break;}
		}
		return $allow;
	}
	
	function get_trees($parent_id = 0,$space = ' ',$trees = NULL)
	{
		if(!$trees) $trees = array();
		$this->db->select()
				 ->from($this->_table)
				 ->where(array('parent_id' => intval($parent_id)));
		$this->db->order_by('order ASC');
		$q = $this->db->get();
        $arr = $q->result();
        foreach($arr as $k => $rs){
            if($parent_id==0){
            	$obj = NULL;
            	$obj->id = $rs->id;
            	$obj->parent_id = $rs->parent_id;
            	$obj->name = $rs->name;
            	$obj->order = $rs->order;
            	$obj->title = $rs->title;
            	$obj->status = $rs->status;
                $trees[] = $obj;
            }else{
                $obj = NULL;
                $obj->id = $rs->id;
                $obj->parent_id = $rs->parent_id;
            	$obj->name = $rs->name;
            	$obj->order = $rs->order;
            	$obj->title = $space.$rs->title;
            	$obj->status = $rs->status;
                $trees[] = $obj;
            }

            $trees = $this->get_trees($rs->id,$space,$trees);
        }
        return $trees;
	}
}